package com.bainuo.doctor.common.image_support.imghandle.uploader;


import com.bainuo.doctor.common.network_engine.UpFileServerApi;

import java.io.File;
import java.io.Serializable;

import static com.bainuo.doctor.common.image_support.imghandle.uploader.UploadTask.TYPE.IMAGE;

/**
 * Created by bainuo on 2017/2/14.
 */

public class UploadTask implements Serializable{

    enum TYPE {
        IMAGE,VOICE
    }

    private TYPE type;

    public boolean isImageType() {
        return type == IMAGE;
    }

    public void setImageType() {
        type = IMAGE;
    }

    /**
     * 自增长 id
     */
    static int increaseId = 0;

    /**
     * 文件准备上传
     */
    public static final int STATE_PRE = 0;

    /**
     * 文件上传中
     */
    public static final int STATE_ING = 1;

    /**
     * 文件上传完成
     */
    public static final int STATE_COM = 2;

    /**
     * 文件上传失败
     */
    public static final int STATE_FAILED = 3;

    public static final int STATE_CANCEL = 4;



    /**
     * 任务状态
     */
    private int state;

    /**
     * 任务进度
     */
    private int progress;

    /**
     * 文件路径
     */
    private String uploadPath;

    /**
     * 上传返回的url
     */
    private boolean deleteOnUploaded;

    private String url;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        if ((deleteOnUploaded && state == STATE_COM) || state == STATE_CANCEL){
            if (uploadPath != null && !uploadPath.equals(srcPath)){
                //如果是原图则不删除
                new File(uploadPath).delete();
            }
        }
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public boolean isDeleteOnUploaded() {
        return deleteOnUploaded;
    }

    public void setDeleteOnUploaded(boolean deleteOnUploaded) {
        this.deleteOnUploaded = deleteOnUploaded;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * 任务id
     */
    public String id;

    public String getSrcPath() {
        return srcPath;
    }

    public void setSrcPath(String srcPath) {
        this.srcPath = srcPath;
    }

    private String srcPath;


    public String getUploadType() {
        return uploadType;
    }

    public void setUploadType(String uploadType) {
        this.uploadType = uploadType;
    }

    private String uploadType = UpFileServerApi.TYPE_DOCTOR;

    /**
     * 生成任务id
     */
    public UploadTask() {
        init();
    }

    private void init() {
        id = "" + increaseId++;//UUID.randomUUID().toString();
        state = STATE_PRE;
        deleteOnUploaded = true;
    }

    public UploadTask(String path) {
        init();
        uploadPath = path;
    }

    public boolean isUploadComplete() {
        return getUrl() != null;
    }
}
